/**
 * TimeStamper 1.0
 * Copyright 2009 Zach Scrivena
 * 2009-02-28
 * zachscrivena@gmail.com
 * http://code.google.com/p/java-misc-command-line-utils/
 *
 * Display a timestamp for the current time, formatted according to a
 * specified pattern.
 *
 * TERMS AND CONDITIONS:
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.freeshell.zs.timestamper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class TimeStamper
{
	/**
	 * Main entry point to the program.
	 *
	 * @param args
	 *     command-line arguments
	 */
	public static void main(
			final String[] args)
	{
		if (args.length == 0)
		{
			System.err.println(
					"\nTimeStamper 1.0   Copyright 2009 Zach Scrivena   2009-02-28" +
					"\n" +
					"\nDisplay a timestamp for the current time, formatted according to a" +
					"\nspecified pattern." +
					"\n" +
					"\nUSAGE: java -jar TimeStamper.jar  [\"Pattern\"]" +
					"\n" +
					"\n  [\"Pattern\"]: Pattern string for the timestamp format" +
					"\n" +
					"\n    G  Era designator            H  Hour in day (0-23)" +
					"\n    y  Year                      k  Hour in day (1-24)" +
					"\n    M  Month in year             K  Hour in AM/PM (0-11)" +
					"\n    w  Week in year              h  Hour in AM/PM (1-12)" +
					"\n    W  Week in month             m  Minute in hour" +
					"\n    D  Day in year               s  Second in minute" +
					"\n    d  Day in month              S  Millisecond" +
					"\n    F  Day of week in month      z  Time zone (general time zone)" +
					"\n    E  Day in week               Z  Time zone (RFC 822 time zone)" +
					"\n    a  AM/PM marker" +
					"\n" +
					"\n    Repeat letters to change the representation, e.g. use MMMM, MMM, MM, M" +
					"\n    for \"April\", \"Apr\", \"04\", \"4\", respectively." +
					"\n    String literals can be escaped by enclosing them in 'single quotes'." +
					"\n    The following presets are also defined:" +
					"\n" +
					"\n    <D1>  yyyy-MM-dd             <T1>  HHmmss" +
					"\n    <D2>  yyyyMMdd               <T2>  HHmmssSSS" +
					"\n" +
					"\nEXAMPLES:" +
					"\n" +
					"\n  1. Display \"2009-02-28 170301\":" +
					"\n     java -jar TimeStamper.jar  \"yyyy-MM-dd HHmmss\"" +
					"\n     java -jar TimeStamper.jar  \"<D1> <T1>\"" +
					"\n" +
					"\n  2. Display \"2001.07.04 AD at 12:08:56 PDT\":" +
					"\n     java -jar TimeStamper.jar  \"yyyy.MM.dd G 'at' HH:mm:ss z\"" +
					"\n");

			return;
		}

		/* get pattern string from command-line arguments */
		final StringBuilder sb = new StringBuilder(args[0]);

		for (int i = 1; i < args.length; i++)
		{
			sb.append(' ');
			sb.append(args[i]);
		}

		String pattern = sb.toString();

		/* substitute presets */
		pattern = pattern.replace("<D1>", "yyyy-MM-dd");
		pattern = pattern.replace("<D2>", "yyyyMMdd");
		pattern = pattern.replace("<T1>", "HHmmss");
		pattern = pattern.replace("<T2>", "HHmmssSSS");

		/* create timestamp for the current time */
		try
		{
			final SimpleDateFormat f = new SimpleDateFormat(pattern, Locale.ENGLISH);
			final String s = f.format(new Date());
			System.out.print(s);
		}
		catch (Exception e)
		{
			String msg = e.getMessage();
			if (!msg.endsWith(".")) msg += ".";

			System.err.println(
					"ERROR: " + msg +
					"\nTo display help, run TimeStamper without any command-line arguments." +
					"\n");

			e.printStackTrace();
			System.exit(1);
		}
	}
}
